package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.itheima.common.vo.Result;
import com.itheima.mapper.MemberMapper;
import com.itheima.model.LineDataModel;
import com.itheima.service.ReportService;
import org.springframework.beans.factory.annotation.Autowired;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

@Service(version = "1.0",interfaceClass = ReportService.class)
public class ReportServiceImpl implements ReportService {
    @Autowired
    private MemberMapper memberMapper;
    @Override
    public Result getMemberReport() {
        /**
         * 1. 获取前12个月的日期
         * 2. 通过日期 去查询 会员数
         */
        LineDataModel lineDataModel = new LineDataModel();

        Calendar instance = Calendar.getInstance();
        instance.add(Calendar.MONTH,-12);
        List<String> months = new ArrayList<>();
        List<Integer> memberCounts = new ArrayList<>();
        for (int i = 0;i < 12;i++){
            instance.add(Calendar.MONTH,1);
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
            String format = simpleDateFormat.format(instance.getTime());
            months.add(format);
            Integer memberCount = this.memberMapper.countMemberBeforeDate(format);
            memberCounts.add(memberCount);
        }
        lineDataModel.setXData(months);
        lineDataModel.setYData(memberCounts);
        return Result.success(lineDataModel);
    }

    public static void main(String[] args) {
        Calendar instance = Calendar.getInstance();
        instance.add(Calendar.MONTH,-12);
        List<String> months = new ArrayList<>();
        for (int i = 0;i < 12;i++){
            instance.add(Calendar.MONTH,1);
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
            System.out.println(simpleDateFormat.format(instance.getTime()));
        }
    }
}
